package com.hibernate.daoimp.news;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.hibernate.dao.news.ArticlesServiceDao;
import com.hibernate.pojo.Articles;
import com.struts.form.ArticlesForm;
import com.common.Constant;
import com.common.Page;

public class ArticlesServiceDaoImp extends HibernateDaoSupport implements ArticlesServiceDao
{
	private boolean bool;
	private List list;
	public int record_count=0;
	/**
	 * save article
	 */
	public boolean saveNews(Articles articles)
	{
		bool=false;
		try
		{
			articles.setCreate_time(new Date());
			this.getHibernateTemplate().save(articles);
			bool=true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			return bool;
		}
	}
	
	/**
	 *
	 */
	public boolean updateNewsAudit(ArticlesForm articlesform)
	{
		bool=false;
		try
		{
			Articles articles=(Articles)this.getHibernateTemplate().load(Articles.class, articlesform.getId());
			articles.setAudit(articlesform.getAudit());
			this.getHibernateTemplate().update(articles);
			bool=true;
		}
		catch(Exception e)
		{e.printStackTrace();}
		finally
		{
			return bool;
		}
	}

	
	/**
	 * 
	 */
	public Map updateNewsView(Long id)
	{
		Map hashmap=new HashMap();
		Articles articles=new Articles();
		articles=(Articles)this.getHibernateTemplate().get(Articles.class, id);
		hashmap.put("update_news", articles);
		return hashmap;
	}
	
	/**
	 * update article
	 */
	public boolean updateNews(ArticlesForm articlesform,String newfilename)
	{
		bool=false;
		try
		{
			Articles articles=(Articles)this.getHibernateTemplate().load(Articles.class, articlesform.getId());
			articles.setTitle(articlesform.getTitle());
			articles.setKeyword(articlesform.getKeyword());
			articles.setIntr(articlesform.getIntr());
			articles.setContent(articlesform.getContent());
			articles.setAuthor(articlesform.getAuthor());
			articles.setLeibie(articlesform.getLeibie());
			articles.setRecommend(articlesform.getRecommend());
			articles.setEdit_time(new Date());
			if(!newfilename.equals(""))
			{
				articles.setImgUrl(newfilename);
			}
			this.getHibernateTemplate().update(articles);
			bool=true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			return bool;
		}
	}
	
	/**
	 * delete article
	 * parameter1 article id	 
	 */
	public boolean deleteNews(Long id)
	{
		bool=false;
		try
		{
			Articles articles=(Articles)this.getHibernateTemplate().load(Articles.class, id);
			this.getHibernateTemplate().delete(articles);
			bool=true;
		}
		catch(Exception e)
		{e.printStackTrace();}
		finally
		{
			return bool;
		}
	}
	
	/**
	 * read articles list
	 * parameter1 page number,parameter2 article type id
	 */
	public Map findNews(int page, final int articlesleibieID)
	{
		Map hashmap=new HashMap();
		record_count=page*Constant.PAGE_VIEW_COUNT;
		if(articlesleibieID==0)
		{
			list=this.getHibernateTemplate().find("select count(*) from Articles");
		}
		else
		{
			list=this.getHibernateTemplate().find("select count(*) from Articles where leibie="+articlesleibieID);
		}
		Integer counts=(Integer)list.get(0);
		Page pageobject=new Page();	
		Map map=pageobject.pageCount(counts);
		map.put("pagecur", page);
		map.put("articlesleibieID",articlesleibieID);
		hashmap.put("page_content", map);
		list=getHibernateTemplate().executeFind(new HibernateCallback(){
		public Object doInHibernate(Session session)throws HibernateException, SQLException
		{
			String hql_news;
			Query query;
			if(articlesleibieID==0)
			{
				hql_news=" from Articles order by ID desc";
				query = session.createQuery(hql_news);
				query.setFirstResult(record_count);
				query.setMaxResults(Constant.PAGE_VIEW_COUNT);
			}
			else
			{
				hql_news=" from Articles where leibie=? order by ID desc";
				query = session.createQuery(hql_news);
				query.setInteger(0, articlesleibieID);
				query.setFirstResult(record_count);
				query.setMaxResults(Constant.PAGE_VIEW_COUNT);
			}
				List list = query.list();
				return list;
		}
		});
		hashmap.put("news_list", list);
		return hashmap;
	}
	
	/**
	 * ͨ��HQL����������
	 */
	public List findNewsTypes(String hql)
	{
		list=this.getHibernateTemplate().find(hql);
		return list;
	}
	
	public boolean deleteBatchNews(String ids)
	{
		bool=false;
		final String hql="delete from Articles where id in ("+ids+")";
		this.getHibernateTemplate().execute(new HibernateCallback(){
		public Object doInHibernate(Session session)throws HibernateException, SQLException
		{
			Query query = session.createQuery(hql);
			int n=query.executeUpdate();
			bool=true;
			return bool;
		}
		});
		return bool;
	}
	
	public boolean updateBatchNewsLeibie(String ids,Long categry)
	{
		bool=false;
		final String hql="update Articles set leibie="+categry+" where id in ("+ids+")";
		this.getHibernateTemplate().execute(new HibernateCallback(){
		public Object doInHibernate(Session session)throws HibernateException, SQLException
		{
			Query query = session.createQuery(hql);
			int n=query.executeUpdate();
			bool=true;
			return bool;
		}
		});
		return bool;
	}
}
